package com.cspg.ssss.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cspg.ssss.domain.DwdggbmjdDataEntity;
import com.cspg.ssss.domain.DwdggbmjdTemporaryEntity;
import com.cspg.ssss.domain.UserDetailInfoEntity;
import com.cspg.ssss.service.DwdggbmjdDataService;
import com.cspg.ssss.service.DwdggbmjdTemporaryService;
import com.cspg.ssss.service.WxDepartmentService;
import com.cspg.ssss.vo.ResultVo;
import com.cspg.ssss.vo.ResultVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/dwzsimport")
public class DwImportDataController {
    @Autowired
    private DwdggbmjdDataService dwdggbmjdDataService;
    @Autowired
    private DwdggbmjdTemporaryService dwdggbmjdTemporaryService;
    @Autowired
    private WxDepartmentService departmentService;

    @RequestMapping(value = "/selectByid", method = RequestMethod.POST)
    public ResultVo selectByid(@RequestParam String id) {
        List<DwdggbmjdDataEntity> s = dwdggbmjdDataService.selectDataById(id);
        if (s.size() > 0) {
            return ResultVoUtil.success("查询成功", s, "400");
        } else {
            return ResultVoUtil.error("0000", "查询失败");
        }
    }

    @RequestMapping(value = "/UpdateByid", method = RequestMethod.POST)
    public ResultVo UpdateByid(@RequestBody DwdggbmjdDataEntity dwdggbmjdDataEntity) {
        try {
            HashMap<String, Object> condition = new HashMap<>();
            condition.put("userid", dwdggbmjdDataEntity.getPersonName());
            List<UserDetailInfoEntity> departmentList = departmentService.getDepartmentUserList(condition);
            if (departmentList != null && departmentList.size() > 0) {
                dwdggbmjdDataEntity.setDepartmentName(departmentList.get(0).getDepartment_displaypath());
            }
            String c = dwdggbmjdDataEntity.getWtfxfs() + dwdggbmjdDataEntity.getDwmc() + dwdggbmjdDataEntity.getDepartmentName() +
                    dwdggbmjdDataEntity.getWtms() + dwdggbmjdDataEntity.getWtfxfs();
            //加密生成唯一id
            String id = DigestUtils.md5DigestAsHex(c.getBytes());
            DwdggbmjdTemporaryEntity dw = dwdggbmjdTemporaryService.selectDataId(id);
            if (dw != null) {
                String xh = dw.getXh();
                dwdggbmjdDataEntity.setIsover("1");
                return ResultVoUtil.error("0000", "重复数据", dwdggbmjdDataEntity);
            }
            int s = dwdggbmjdDataService.updateData(dwdggbmjdDataEntity);
            if (s > 0) {
                return ResultVoUtil.success("修改成功", "", "400");
            } else {
                return ResultVoUtil.error("9999", "修改失败，未修改数据");
            }
        } catch (Exception e) {
            return ResultVoUtil.error("1111", "修改失败");
        }
    }

    @RequestMapping(value = "/addMore", method = RequestMethod.POST)
    public ResultVo AddMore(@RequestBody JSONArray jSONArray) {
        String v = JSONArray.toJSONString(jSONArray);
        List<DwdggbmjdDataEntity> list = JSON.parseArray(v, DwdggbmjdDataEntity.class);
        List<DwdggbmjdTemporaryEntity> list2 = JSON.parseArray(v, DwdggbmjdTemporaryEntity.class);
        List<String> list4 = new ArrayList<>();
        int count = dwdggbmjdDataService.selectDataCount();
        int a = 0;
        for (int i = 0; i < list.size(); i++) {
            count = count + 1;
            String c = Integer.toString(count);
            list.get(i).setXh(c);
            DwdggbmjdTemporaryEntity dwdggbmjdTemporaryEntity = dwdggbmjdTemporaryService.selectDataId(list.get(i).getId());
            if (dwdggbmjdTemporaryEntity == null) {
                return ResultVoUtil.error("3333", "警告，只能插入临时数据存在的数据");
            }
            //使用问题发现方式，单位名称，部门名称，问题描述，问题发现时间进行字符串组合
            String s = list.get(i).getWtfxfs() + list.get(i).getDwmc() + list.get(i).getDepartmentName() +
                    list.get(i).getWtms() + list.get(i).getWtfxsj();
            //加密生成唯一id
            String id = DigestUtils.md5DigestAsHex(s.getBytes());
            list.get(i).setId(id);
            if (list4.size() > 0) {
                for (int j = 0; j < list4.size(); j++) {
                    if (id.equals(list4.get(j))) {
                        list.get(i).setIsover("1");
                        a = a + 1;
                    }
                }
            }
            list4.add(id);
            DwdggbmjdDataEntity dwdggbmjdDataEntity = dwdggbmjdDataService.selectDataId(id);
            if (dwdggbmjdDataEntity != null) {
                a = a + 1;
                list.get(i).setIsover("1");
                return ResultVoUtil.error("0000", "临时数据插入正式数据库失败，含有重复问题", list);
            }
            //2021-06-22 ybq 添加提单时间
            list.get(i).setCreateaTime(new Date());
        }
        if (a > 0) {
            return ResultVoUtil.error("0000", "临时数据插入正式数据库失败，含有重复问题", list);
        }
        int c = dwdggbmjdDataService.batchInsert(list);
        if (c > 0) {
            int s = dwdggbmjdTemporaryService.deleteByList(list2);
            if (s > 0) {
                return ResultVoUtil.success("临时数据插入正式数据库成功", "", "400");
            } else {
                return ResultVoUtil.error("9999", "未知异常，请联系管理员");
            }
        } else {
            return ResultVoUtil.error("0000", "临时数据插入正式数据库失败，含有重复问题", list);
        }
    }

    @RequestMapping(value = "/selectDateAll", method = RequestMethod.GET)
    public ResultVo selectAll(String id) {
        List<DwdggbmjdDataEntity> list = dwdggbmjdDataService.selectAll(id);
        if (list.size() > 0) {
            return ResultVoUtil.success("查询成功", list, "400");
        } else {
            return ResultVoUtil.error("1111", "查询成功,您没有上传正式数据");
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResultVo delete(String id) {
        int s = dwdggbmjdDataService.delete(id);
        if (s > 0) {
            return ResultVoUtil.success("删除成功", "", "400");
        } else {
            return ResultVoUtil.error("0000", "删除失败");
        }
    }
}
